package com.myproject.thread.interrupt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.LockSupport;

public class InterruptState1 {

    private static final Logger logger = LoggerFactory.getLogger(InterruptState1.class);
    private static volatile boolean flag = false;

    public static void main(String[] args) throws InterruptedException {

        //sleep wait join会清除interrupt状态，但park不会
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag){}
                LockSupport.park();
                System.out.println(Thread.currentThread() + " " + Thread.interrupted());
            }
        });
        thread.start();
        Thread.sleep(1500);
        flag=true;
        thread.interrupt();
        flag=false;

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag){}
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + " " + Thread.interrupted());
            }
        });
        t2.start();
        Thread.sleep(1500);
        flag=true;
        t2.interrupt();
    }
}
